import Vue from "vue";
import VueRouter from "vue-router";

if (process.env.NODE_ENV === "development") {
    Vue.use(VueRouter);
    // 解决报错
    const originalPush = VueRouter.prototype.push;
    const originalReplace = VueRouter.prototype.replace;
    // push
    VueRouter.prototype.push = function push (location, onResolve, onReject) {
      if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
      return originalPush.call(this, location).catch(err => err)
    }
    // replace
    VueRouter.prototype.replace = function push (location, onResolve, onReject) {
      if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
      return originalReplace.call(this, location).catch(err => err)
    }
}

import { ROUTER_MODE } from "../config/app";

import Home from "../views/home/index.vue";
// 首页
const main = r => require.ensure([], () => r(require("../views/charts/charts.vue")), "home");

// 系统
const sysArea = r => require.ensure([], () => r(require("../views/system/sysArea.vue")), "home");
const institution = r => require.ensure([], () => r(require("../views/system/institution.vue")), "home");
const authAdmin = r => require.ensure([], () => r(require("../views/system/authAdmin.vue")), "home");
const authRole = r => require.ensure([], () => r(require("../views/system/authRole.vue")), "home");
const authPermissionRule = r => require.ensure([], () => r(require("../views/system/authPermissionRule.vue")), "home");
const appVersion = r => require.ensure([], () => r(require("../views/system/appVersion.vue")), "home");

const err401 = r => require.ensure([], () => r(require("../views/error/err401.vue")), "home");
const err404 = r => require.ensure([], () => r(require("../views/error/err404.vue")), "home");
const login = r => require.ensure([], () => r(require("../views/login/index.vue")), "home");

// 需求
const needs = r => require.ensure([], () => r(require("../views/needs/needs.vue")), "home");

// 发布
const publish = r => require.ensure([], () => r(require("../views/publish/publish.vue")), "home");

// 智库
const thinktank = r => require.ensure([], () => r(require("../views/thinktank/thinktank.vue")), "home");

// 用户
const user = r => require.ensure([], () => r(require("../views/user/user.vue")), "home");
const tuisongxiaoxi = r => require.ensure([], () => r(require("../views/user/tuisongxiaoxi/tuisongxiaoxi.vue")), "home");
const fankui = r => require.ensure([], () => r(require("../views/user/fankui/fankui.vue")), "home");

// 空间管理
const spaceManagement = r => require.ensure([], () => r(require("../views/spaceManagement/index.vue")), "home");
const addSpace = r => require.ensure([], () => r(require("../views/spaceManagement/space/addSpace.vue")), "home");
const spaceDetail = r => require.ensure([], () => r(require("../views/spaceManagement/space/spaceDetail.vue")), "home");
const companyManage = r => require.ensure([], () => r(require("../views/spaceManagement/company/companyManage.vue")), "home"); // 入驻企业管理
const addCompany = r => require.ensure([], () => r(require("../views/spaceManagement/company/addCompany.vue")), "home");
const companyDetail = r => require.ensure([], () => r(require("../views/spaceManagement/company/companyDetail.vue")), "home");
const spaceList = r => require.ensure([], () => r(require("../views/spaceManagement/spaceList/spaceList.vue")), "home"); // 空间下工位、房间、会议室的管理
const spaceListDetail = r => require.ensure([], () => r(require("../views/spaceManagement/spaceList/spaceListDetail.vue")), "home");
const huiyishiList = r => require.ensure([], () => r(require("../views/spaceManagement/huiyishiList/index.vue")), "home"); // 会议室管理
const gongShangZhuCe = r => require.ensure([], () => r(require("../views/spaceManagement/gongShangZhuCe/gongshangzhuce.vue")), "home"); // 工商注册

// 服务
const server = r => require.ensure([], () => r(require("../views/server/index.vue")), "home");

// 常用资料
const changyongziliao = r => require.ensure([], () => r(require("../views/changyongziliao/index.vue")), "home");

// 活动
const huodong = r => require.ensure([], () => r(require("../views/huodong/huodong.vue")), "home");

// 注意 权限字段 authRule （严格区分大小写）
export const constantRouterMap = [
    {
        path: "*",
        component: err404,
        hidden: true
    },
    {
        path: "/401",
        component: err401,
        name: "401",
        hidden: true
    },
    {
        path: "/404",
        component: err404,
        name: "404",
        hidden: true
    },
    {
        path: "/500",
        component: err404,
        name: "500",
        hidden: true
    },
    {
        path: "/login",
        component: login,
        name: "登录",
        hidden: true
    },
    {
        path: "/",
        component: Home,
        redirect: "/index",
        name: "首页",
        hidden: true
    }
];

export default new VueRouter({
    // mode: 'history', //后端支持可开
    mode: ROUTER_MODE,
    routes: constantRouterMap,
    strict: process.env.NODE_ENV !== "production",
    linkActiveClass:'myActive'
});

export const asyncRouterMap = [{
        path: "/index",
        component: Home,
        redirect: "/index/index",
        name: "首页",
        children: [{
            path: "index",
            name: "概览",
            component: main
        }]
    },{
        path: "/system",
        component: Home,
        redirect: "/system/sysArea",
        icon: "shouye",
        name: "系统设置",
        noDropdown: true,
        meta: {authRule: ["system"]},
        children: [{
            path: "admin",
            name: "管理员",
            meta: {authRule: ["user"]},
            component: authAdmin
        },{
            path: "role",
            name:"角色管理",
            meta: {authRule: ["role"]},
            component: authRole
        },{
            path: "permission",
            name:"权限管理",
            meta: {authRule: ["permission"]},
            component: authPermissionRule
        },{
            path: "appVersion",
            name:"app版本",
            meta:{authRole: ["appversion"]},
            component: appVersion
        }]
    },{
        path: "/publish",
        component: Home,
        redirect: "/publish/publish",
        name: "文章",
        noDropdown: true,
        meta: {authRule:["publish"]},
        children: [{
            path: "publish",
            name: "文章",
            meta: {authRule:["publish"]},
            component: publish
        }]
    },{
        path: "/thinktank",
        component: Home,
        redirect: "/thinktank/thinktank",
        name: "智库",
        noDropdown: true,
        meta: {authRule:["thinktank"]},
        children: [{
            path: "thinktank",
            name: "智库",
            meta: {authRule:["thinktank"]},
            component: thinktank
        }]
    },{
        path: "/needs",
        component: Home,
        redirect: "/needs/needs",
        name: "需求",
        noDropdown: true,
        meta: {authRule:["needs"]},
        children: [{
            path: "needs",
            name: "需求",
            meta: {authRule:["needs"]},
            component: needs
        }]
    },{
        path: "/user",
        component: Home,
        redirect: "/user/user",
        name: "用户",
        noDropdown: true,
        meta: {authRule:["yonghu"]},
        children: [{
            path: "user",
            name: "用户",
            meta: {authRule:["yonghu"]},
            component: user
        },{
            path: 'tuisongxiaoxi',
            name: '推送消息',
            meta: {authRule:["tuisongxiaoxi"]},
            component: tuisongxiaoxi
        },{
            path: 'fankui',
            name: '反馈与建议',
            meta: {authRule:["fankui"]},
            component: fankui
        }]
    },{
        path: "/spaceManagement",
        component: Home,
        redirect: "/spaceManagement/spaceManagement",
        name: "空间管理",
        noDropdown: true,
        meta: {authRule:["space"]},
        children: [{
            path: "spaceManagement",
            name: "空间管理",
            // meta: {authRule:["space"]},
            component: spaceManagement
        },{
            path: "addSpace",
            // meta: {authRule:["addSpace"]},
            component: addSpace
        },{
            path: "spaceDetail",
            // meta: {authRule:["spaceDetail"]},
            component: spaceDetail
        },{
            path: "companyManage",
            name: "入驻企业管理",
            // meta: {authRule:["companyManage"]},
            component: companyManage
        },{
            path: "addCompany",
            // meta: {authRule:["addCompany"]},
            component: addCompany
        },{
            path: "companyDetail",
            // meta: {authRule:["companyDetail"]},
            component: companyDetail
        },{
            path: "spaceList",
            name: "工位房间管理",
            // meta: {authRule:["spaceList"]},
            component: spaceList
        },{
            path: "spaceListDetail",
            // meta: {authRule:["spaceListDetail"]},
            component: spaceListDetail
        },{
            path: "huiyishiList",
            name: "会议室管理",
            // meta: {authRule:["spaceList"]},
            component: huiyishiList
        },{
            path: "gongShangZhuCe",
            name: "工商注册",
            // meta: {authRule:["gszc"]},
            component: gongShangZhuCe
        }]
    },{
        path: "/server",
        component: Home,
        redirect: "/server/server",
        name: "第三方服务",
        noDropdown: true,
        meta: {authRule:["third"]},
        children: [{
            path: "server",
            name: "第三方服务",
            meta: {authRule:["third"]},
            component: server
        }],
    },{
        path: "/changyongziliao",
        component: Home,
        redirect: "/changyongziliao/changyongziliao",
        name: "常用资料",
        noDropdown: true,
        meta: {authRule:["changyongziliao"]},
        children: [{
            path: "changyongziliao",
            name: "常用资料",
            meta: {authRule:["changyongziliao"]},
            component: changyongziliao
        }],
    },{
        path: "/huodong",
        component: Home,
        redirect: "/huodong/huodong",
        name: "活动",
        noDropdown: true,
        meta: {authRule:["huodong"]},
        children: [{
            path: "huodong",
            name: "活动",
            meta: {authRule:["huodong"]},
            component: huodong
        }],
    }
];
